package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhEmployment;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhEmploymentService;
import com.rs.service.ZhUserStudentService;
import com.rs.util.SpringContextUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.DefaultComboBoxModel;

public class EmploymentInfo extends JFrame {

	int state=1;//1为 添加 2 为修改
	int id;
	
	JDesktopPane desktopPane_1;
	SelectEmployment ss;
	
	private JPanel contentPane;
	private JTextField text_userName;
	private JTextField text_enterprise;
	private JTextField text_address;
	private JTextField text_salary;
	private JTextField text_phone;
	private JComboBox comboBox_class;
	
	private ZhEmploymentService emp = SpringContextUtils.getBean(ZhEmploymentService.class);
	private ZhUserStudentService zhUserService  = SpringContextUtils.getBean(ZhUserStudentService.class);
	private ZhClassesService classesService = SpringContextUtils.getBean(ZhClassesService.class);

	Map<String,Integer> mapcl = new HashMap<>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EmploymentInfo frame = new EmploymentInfo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EmploymentInfo() {
		aa();
	}
	
	public EmploymentInfo(Integer id,JDesktopPane desktopPane_1,SelectEmployment ss) {
		this.desktopPane_1 = desktopPane_1;
		this.ss=ss;
		this.id = id;
		state=2;
		aa();
	}
	void aa(){
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 528, 413);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label_userName = new JLabel("姓名：");
		label_userName.setHorizontalAlignment(SwingConstants.RIGHT);
		label_userName.setBounds(55, 91, 74, 26);
		contentPane.add(label_userName);
		
		JLabel label_class = new JLabel("班级：");
		label_class.setHorizontalAlignment(SwingConstants.RIGHT);
		label_class.setBounds(55, 144, 74, 26);
		contentPane.add(label_class);
		
		JLabel label_enterprise = new JLabel("企业名称：");
		label_enterprise.setHorizontalAlignment(SwingConstants.RIGHT);
		label_enterprise.setBounds(263, 197, 74, 26);
		contentPane.add(label_enterprise);
		
		JLabel label_address = new JLabel("就业地址：");
		label_address.setHorizontalAlignment(SwingConstants.RIGHT);
		label_address.setBounds(263, 91, 74, 26);
		contentPane.add(label_address);
		
		JLabel label_salary = new JLabel("就业薪资：");
		label_salary.setHorizontalAlignment(SwingConstants.RIGHT);
		label_salary.setBounds(263, 144, 74, 26);
		contentPane.add(label_salary);
		
		text_userName = new JTextField();
		text_userName.setBounds(130, 94, 123, 26);
		contentPane.add(text_userName);
		text_userName.setColumns(10);
		
		text_enterprise = new JTextField();
		text_enterprise.setColumns(10);
		text_enterprise.setBounds(341, 198, 123, 26);
		contentPane.add(text_enterprise);
		
		text_address = new JTextField();
		text_address.setColumns(10);
		text_address.setBounds(341, 94, 123, 26);
		contentPane.add(text_address);
		
		text_salary = new JTextField();
		text_salary.setColumns(10);
		text_salary.setBounds(341, 147, 123, 26);
		contentPane.add(text_salary);
		
		
		
		JLabel label_phone = new JLabel("手机号：");
		label_phone.setHorizontalAlignment(SwingConstants.RIGHT);
		label_phone.setBounds(55, 203, 74, 26);
		contentPane.add(label_phone);
		
		text_phone = new JTextField();
		text_phone.setColumns(10);
		text_phone.setBounds(130, 200, 123, 26);
		contentPane.add(text_phone);
		
		comboBox_class = new JComboBox();
		comboBox_class.setModel(new DefaultComboBoxModel(new String[] {"无"}));
		comboBox_class.setBounds(130, 144, 123, 26);
		contentPane.add(comboBox_class);
		
		List<ZhClasses> list = classesService.listZhClasses();
		for(ZhClasses c:list){
			comboBox_class.addItem(c.getClName());
			mapcl.put(c.getClName(), c.getClId());
		}
		
		if(state ==2){
			setValue();
			text_userName.setEditable(false);
			text_phone.setEditable(false);
			JButton button = new JButton("提交修改");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					updateSubmit();
					
				}
			});
			button.setBounds(341, 310, 93, 23);
			contentPane.add(button);
		}else if(state ==1){
			
			JButton button_1 = new JButton("提交");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
							submit();
				}
			});
			button_1.setBounds(341, 277, 93, 23);
			contentPane.add(button_1);
		}
	}
	
	void close(){
		this.dispose();
	}
	private void submit(){
		ZhEmployment ze = getZhEmployment();
		CommonResult<Integer> com = emp.addZhEmployment(ze);
		if(com.getData()>0){
			this.dispose();
		}
		alert(com.getMessage());
	}
	
	private void updateSubmit(){
		ZhEmployment ze = getZhEmployment();
		
		CommonResult<Integer> com = emp.updateZhEmployment(ze);
		alert(com.getMessage());
		if(com.getData()>0){
			SelectEmployment se = new SelectEmployment(desktopPane_1);
			se.setVisible(true);
			desktopPane_1.removeAll();
			desktopPane_1.add(se);
		}
		
	}
	
	//将表格包装为类
	private ZhEmployment getZhEmployment(){
		if(text_phone.getText() ==null){
			alert("手机号不能为空！！");
			return null;
		}
		ZhEmployment ze = new ZhEmployment();
		if(state ==1){
			Integer uid = getUserId(text_phone.getText());
			if( uid==null){
				return null;
			}
			ze.setEmUid(uid);
		}else if(state ==2){
			ze.setEmId(id);
		}
		
		if(!comboBox_class.getSelectedItem().equals("无")){
			ze.setEmClaid(mapcl.get(comboBox_class.getSelectedItem()));
		}
		ze.setEmAddress(text_address.getText());
		if(!text_salary.getText().equals("")){
			String money = text_salary.getText();
			ze.setEmSalary(Double.parseDouble(money));
			/*money = money.substring(-1,money.lastIndexOf(".")-1);
			Integer a=new Integer(money);
			ze.setEmSalary(a.doubleValue());*/
		}
		ze.setEmEnterprise(text_enterprise.getText());
		return ze;
	}
	
	//根据手机号得到用户id
	private Integer getUserId(String phone){
		Integer id = zhUserService.getUserId(phone);
		if(id == null){
			alert("手机号没有找到！！");
			return null;
		}
		return id;
	}
	
	void alert(String s){
		JOptionPane.showMessageDialog(this, s);
	}
	
	//修改时将数据渲染到页面
	private void setValue(){
		ZhEmployment com = emp.zhEmploymentId(id);
		if(com ==null){
			alert("没找到！！！");
		}
		if(com.getZhUser() != null){
			text_userName.setText(com.getZhUser().getuName());
			text_phone.setText(com.getZhUser().getuPhone());
		}
		if(com.getZhClasses() !=null){
			comboBox_class.setSelectedItem(com.getZhClasses().getClName());
		}
		text_address.setText(com.getEmAddress());
		text_salary.setText(com.getEmSalary()+"");
		text_enterprise.setText(com.getEmEnterprise());
	}
	
}
